#cp /home/vudao/work/projects/trunk/nmath/libnmath/linux/*.h /home/vudao/workspace/PhysicLab/jni/nmath/
#cp /home/vudao/work/projects/trunk/nmath/libnmath/linux/*.c /home/vudao/workspace/PhysicLab/jni/nmath/

# Instruction
#
# ./ndkbuild.sh 
#
# $NDKROOT/ndk-build
#
# End Instruction

if [ "$NDK" = "" ]; then
	echo NDK variable not set, exiting
	echo "Use: export NDK=/your/path/to/android-ndk"
	exit 1
fi

OS=linux

function build_nmath
{
	PLATFORM=$NDK/platforms/$PLATFORM_VERSION/arch-$ARCH/
	CC="$PREBUILT/bin/$EABIARCH-gcc --sysroot=$PLATFORM"
	CROSS_PREFIX=$PREBUILT/bin/$EABIARCH-
	PKG_CONFIG=${CROSS_PREFIX}pkg-config
	NM=$PREBUILT/bin/$EABIARCH-nm
	AR=$PREBUILT/bin/$EABIARCH-ar
	export PKG_CONFIG_LIBDIR=$(pwd)/$PREFIX/lib/pkgconfig/
	export PKG_CONFIG_PATH=$(pwd)/$PREFIX/lib/pkgconfig/
	cd nmath
	$CC -Wall -Werror -fpic -c -o nmbase.o nmbase.c -lm
	$CC -Wall -Werror -fpic -c -o derivative.o derivative.c -lm
	$CC -Wall -Werror -fpic -c -o funct.o funct.c -lm
	$CC -shared -Bsymbolic -lz -ldl -llog -L./ -L$PLATFORM/usr/lib -o $PREFIX/$SONAME nmbase.o derivative.o funct.o -lm $PREBUILT/lib/gcc/$EABIARCH/4.6/libgcc.a
	cd ..
}

#function link_physic {
#	cd nmath
#	PLATFORM=$NDK/platforms/$PLATFORM_VERSION/arch-$ARCH/
#	$PREBUILT/bin/$EABIARCH-ld -rpath-link=$PLATFORM/usr/lib -L$PLATFORM/usr/lib -L$PREFIX/lib  -soname $SONAME -shared -nostdlib  -z,noexecstack -Bsymbolic --whole-archive --no-undefined -o $OUT_LIBRARY -lc -lm -lpthread -lz -ldl -llog  --warn-once  --dynamic-linker=/system/bin/linker -zmuldefs $PREBUILT/lib/gcc/$EABIARCH/4.4.3/libgcc.a || exit 1

#	$PREBUILT/bin/$EABIARCH-ld -rpath-link=$PLATFORM/usr/lib -L$PLATFORM/usr/lib -L$PREFIX/lib  -soname $SONAME -shared -nostdlib  -z,noexecstack -Bsymbolic --whole-archive --no-undefined -o $OUT_LIBRARY --warn-once  --dynamic-linker=/system/bin/linker -zmuldefs $PREBUILT/lib/gcc/$EABIARCH/4.4.3/libgcc.a || exit 1
#	cd ..
#}

#x86
#EABIARCH=i686-linux-android
#ARCH=x86
#OPTIMIZE_CFLAGS="-m32"
#PREFIX=../ffmpeg-build/x86
#OUT_LIBRARY=$PREFIX/libffmpeg.so
#ADDITIONAL_CONFIGURE_FLAG=--disable-asm
#SONAME=libffmpeg.so
#PREBUILT=$NDK/toolchains/x86-4.6/prebuilt/$OS-x86
#PLATFORM_VERSION=android-14
#build_nmath
#link_physic

#mips
#EABIARCH=mipsel-linux-android
#ARCH=mips
#OPTIMIZE_CFLAGS="-EL -march=mips32 -mips32 -mhard-float"
#PREFIX=../ffmpeg-build/mips
#OUT_LIBRARY=$PREFIX/libffmpeg.so
#ADDITIONAL_CONFIGURE_FLAG="--disable-mips32r2"
#SONAME=libffmpeg.so
#PREBUILT=$NDK/toolchains/mipsel-linux-android-4.6/prebuilt/$OS-x86
#PLATFORM_VERSION=android-14
#build_nmath
#link_physic


EABIARCH=arm-linux-androideabi
ARCH=arm
CPU=armv7-a
OPTIMIZE_CFLAGS="-mfloat-abi=softfp -mfpu=vfpv3-d16 -marm -march=$CPU "
PREFIX=../physic-build/armeabi-v7a
OUT_LIBRARY=$PREFIX/libnmath.so
ADDITIONAL_CONFIGURE_FLAG=
SONAME=libnmath.so
PREBUILT=$NDK/toolchains/arm-linux-androideabi-4.6/prebuilt/$OS-x86
PLATFORM_VERSION=android-14
build_nmath
#link_physic

EABIARCH=arm-linux-androideabi
ARCH=arm
CPU=armv7-a
OPTIMIZE_CFLAGS="-mfloat-abi=softfp -mfpu=neon -marm -march=$CPU -mtune=cortex-a8 -mthumb -D__thumb__ "
PREFIX=../physic-build/armeabi-v7a-neon
OUT_LIBRARY=../physic-build/armeabi-v7a/libnmath-neon.so
ADDITIONAL_CONFIGURE_FLAG=--enable-neon
SONAME=libnmath-neon.so
PREBUILT=$NDK/toolchains/arm-linux-androideabi-4.6/prebuilt/$OS-x86
PLATFORM_VERSION=android-14
build_nmath
#link_physic


